package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.PatternMatchingUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;
import com.tencent.cloud.uikit.widget.edittext.TelephoneEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.constant.AccountActivityConst;
import cn.sirun.com.fca.account.constant.AccountIntentConst;
import cn.sirun.com.fca.account.constant.RequestParamConst;
import cn.sirun.com.fca.account.model.base.SubmitPhoneRequest;
import cn.sirun.com.fca.account.model.base.SubmitPhoneResponse;
import cn.sirun.com.fca.account.model.register.SubmitEmailRequest;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterIntentConst;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.IChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.ChangeRegisterAccountPresenter;
import cn.sirun.com.fca.personal.center.view.ISettingRegisterAccountView;

public class SettingRegisterAccountFlowView extends BaseLoadingFlowView<IChangeRegisterAccountPresenter>
        implements ISettingRegisterAccountView {

    private TextView mAccountTV, mNextTV, mOtherRegisterTV, mInputAccountTV;
    private View mLineView;
    private ImageView mAccountIV;
    private LineEditText mAccountET;

    private String mAccount;
    private int mFrom;

    public SettingRegisterAccountFlowView(Activity activity) {
        super(activity);
        mFrom = getActivity().getIntent().getIntExtra(AccountIntentConst.FROM, AccountIntentConst.UNKNOWN);
    }

    public SettingRegisterAccountFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(activity, "");
    }

    @Override
    public void onChangeAccountPhoneResult(BaseResponse<SubmitPhoneResponse> response) {
        hideLoadingView();
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            Intent intent = Router.fromPath(AccountActivityConst.Path.PERSONAL_CAPTCHA);
            intent.putExtra(AccountIntentConst.FROM, mFrom);
            intent.putExtra(AccountIntentConst.TELEPHONE, mAccount);
            ActivityUtils.startActivityForResult(getActivity(), intent);
            return;
        }
        AccountStatusUtils.showToast(getActivity(), code);
    }

    @Override
    protected IChangeRegisterAccountPresenter createPresenter() {
        return new ChangeRegisterAccountPresenter();
    }

    private void initView() {
        setContentView(R.layout.setting_register_account_activity);
        final Activity activity = getActivity();
        mAccountTV = activity.findViewById(R.id.tv_account_prompt);
        mNextTV = activity.findViewById(R.id.tv_next);
        mLineView = activity.findViewById(R.id.v_account_line);
        mAccountIV = activity.findViewById(R.id.iv_account_prompt);
        mAccountET = activity.findViewById(R.id.et_account);
        mOtherRegisterTV = activity.findViewById(R.id.tv_other_register);
        mInputAccountTV = activity.findViewById(R.id.tv_inptu_new_title);

        mAccountET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setConfirmBtnEnable();
            }
        });

        mNextTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkFormat()) {
                    handleNextClick();
                }
            }
        });

        initData();
    }

    private void handleNextClick() {
        showLoadingView();
        if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
            SubmitPhoneRequest request = new SubmitPhoneRequest(RequestParamConst.DEFAULT_NATION_CODE, mAccount);
            getPresenter().changeAccountPhone(request);
        } else {
            SubmitEmailRequest request = new SubmitEmailRequest(mAccount);
            getPresenter().changeAccountEmail(request);
        }
    }

    private void initData() {
        if (mFrom == AccountIntentConst.MOIDFY_BIND_ACCOUNT) {
            mInputAccountTV.setText(R.string.input_setting_mail);
            mAccountTV.setText(R.string.change_bind_email);
        } else if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
            mInputAccountTV.setText(R.string.input_setting_phone);
            mAccountTV.setText(R.string.setting_phone);
        }
    }

    private boolean checkFormat() {
        mAccount = mAccountET.getText().toString();
        boolean isCorrect = false;
        if (mFrom == AccountIntentConst.MOIDFY_BIND_ACCOUNT) {
            isCorrect = PatternMatchingUtils.isEmailCorrect(mAccount);
        } else if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
            isCorrect = PatternMatchingUtils.isTelephoneCorrect(mAccount);
        }
        if (isCorrect) {
            return true;
        } else {
            if (mFrom == AccountIntentConst.MOIDFY_BIND_ACCOUNT) {
                setAccountBg(R.string.setting_mail_error, R.color.login_register_text_color,
                        R.color.login_register_text_color, View.VISIBLE);
            } else if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
                setAccountBg(R.string.setting_phone_error, R.color.login_register_text_color,
                        R.color.login_register_text_color, View.VISIBLE);
            }
            return false;
        }
    }

    private void setConfirmBtnEnable() {
        mAccount = mAccountET.getText().toString();
        int accountLength = mAccountET.length();
        boolean isCorrect = false;
        if (mFrom == AccountIntentConst.MOIDFY_BIND_ACCOUNT) {
            isCorrect = PatternMatchingUtils.isEmailCorrect(mAccount);
        } else if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
            isCorrect = PatternMatchingUtils.isTelephoneCorrect(mAccount);
        }
        if (0 == accountLength || isCorrect) {
            if (mFrom == AccountIntentConst.MOIDFY_BIND_ACCOUNT) {
                setAccountBg(R.string.change_bind_email, R.color.grey_A9,
                        R.color.grey_DD, View.GONE);
            } else if (mFrom == AccountIntentConst.MODIFY_REGISTER_ACCOUNT) {
                setAccountBg(R.string.setting_phone, R.color.grey_A9,
                        R.color.grey_DD, View.GONE);
            }
        }

        if (isCorrect) {
            mNextTV.setBackgroundResource(R.drawable.s_btn_bg_border);
        } else {
            mNextTV.setBackgroundResource(R.drawable.s_btn_default_bg_border);
        }
        mNextTV.setEnabled(isCorrect);
    }

    private void setAccountBg(int text, int textColor, int LineColor, int visibility) {
        mAccountTV.setText(ResourcesUtils.getString(text));
        mAccountTV.setTextColor(ResourcesUtils.getColor(textColor));
        mLineView.setBackgroundColor(ResourcesUtils.getColor(LineColor));
        mAccountIV.setVisibility(visibility);
    }

}
